package code.example.zookeeper.zk;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WatcherTest implements Watcher{
    Logger logger = LoggerFactory.getLogger(WatcherTest.class);
    ConnectTest connectTest = null;

    TriggerEvent triggerEvent;

    public TriggerEvent getTriggerEvent() {
        return triggerEvent;
    }

    public void setTriggerEvent(TriggerEvent triggerEvent) {
        this.triggerEvent = triggerEvent;
    }

    public WatcherTest (ConnectTest connectTest){
        this.connectTest = connectTest;
    }
    @Override
    public void process(WatchedEvent event) {
        Event.EventType eventType = event.getType();
        Event.KeeperState keeperState =  event.getState();
        if(keeperState ==  Event.KeeperState.SyncConnected){//已经连接

            logger.info("成功连接到服务端。connect success");
            this.connectTest.notifyAvailable(true);
        }else if(keeperState == Event.KeeperState.Disconnected){
            this.connectTest.notifyAvailable(false);
            logger.info("连接断开。connect fail ");

        }

        if(eventType == Event.EventType.None){

        }
        triggerEvent.setWatchedEvent(event);
        triggerEvent.printLog();
        switch (eventType){
            case None:
            case NodeCreated:
            case NodeDataChanged:
            case NodeDeleted:
        }
    }
}
